Package nz.co.transparent.client.db

Source Code of nz.co.transparent.client.db.DeleteImportedKeys

/**
* TS Client (http://www.transparent.co.nz)
* Copyright (c) 2004 Transparent Systems Limited
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the /doc/LICENSE.txt
* This is the GNU General Public License Version 2 as published by the Free Software Foundation.
* You can download this program from <a href="http://sourceforge.com/projects/ts-client">http://sourceforge.com/projects/ts-client</a>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License Version 2 for more details.
*
* You should have received a copy of the GNU General Public License
* Version 2 along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*
*/
/*
* Created on Dec 16, 2003
*/
package nz.co.transparent.client.db;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Logger;

import javax.sql.DataSource;

import org.apache.commons.dbutils.DbUtils;


/**
* @author johnz
*/
public class DeleteImportedKeys {

  /**
   * 
   */
  public DeleteImportedKeys() {
    super();
  }

  public void go() {

    Logger log = Logger.getLogger("nz.co.transparent.client.db");
    DataSource dataSource = DataSourceHandler.getDataSource();
    Connection conn = null;
    ResultSet rsetTables = null;
    ResultSet rsetImportedKeys = null;
    Statement stmt = null;
    DatabaseMetaData metaData = null;
    String msg = null;
    String sql = null;

    try {
      conn = dataSource.getConnection();
      stmt = conn.createStatement();
      metaData = conn.getMetaData();
      String[] tableTypes = {"TABLE"};
      rsetTables = metaData.getTables("", "APP", "%", tableTypes);
      String tableName = null;
      while (rsetTables.next()) {
        tableName = rsetTables.getString("TABLE_NAME");
        System.out.println("TABLE_NAME=" + tableName);
        rsetImportedKeys = metaData.getImportedKeys("","APP", tableName);
           
        while (rsetImportedKeys.next()) {
          System.out.println("   FKTABLE_NAME=" + rsetImportedKeys.getString("FKTABLE_NAME"));
          System.out.println("   FKCOLUMN_NAME=" + rsetImportedKeys.getString("FKCOLUMN_NAME"));
          System.out.println("   FK_NAME=" + rsetImportedKeys.getString("FK_NAME"));
          sql = "ALTER TABLE " + tableName + " DROP CONSTRAINT " + rsetImportedKeys.getString("FK_NAME");
          stmt.execute(sql);
        }
      }
    } catch (SQLException se) {
      msg = "SQL error: " + se.getMessage();
      log.warning(msg);
    } finally {
      try {
        DbUtils.close(rsetTables);
        DbUtils.close(rsetImportedKeys);
        DbUtils.close(conn);
      } catch (SQLException se) {
        msg = "SQL error: " + se.getMessage();
        log.warning(msg);
      }
    }
    }

    public static void main(String[] args) {

      new DeleteImportedKeys().go();
    }
  }
TOP

Related Classes of nz.co.transparent.client.db.DeleteImportedKeys

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.